// pages/staff_work_manager/staff_work_manager.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { 
    getCategoryList, 
    getStaffWorkList, 
    getWorkDetail, 
    deletaGoodsItem, 
    getStaffDetail, 
    recommendItem,
     
} from '../../static/api/api.js'
let app = new getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        staffData:null,
        staffId:'',
        pageType:'',
        pageStatus:'1',
        category:null,
        curCatgoryId:'',
        pageLoad:false,
        workList: null,
        isNoMore:false,
        curStatus:1,
        pages: {
            curPage: 1,
            totalPage: 1
        },
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        this.setData({
            'staffId': options.id
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            workList: null
        })
        this.getStaffDetail()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    getStaffDetail() {
        let that = this;
        wx.showLoading({
            title: '加载中...',
        })
        getStaffDetail({ store_user_id: this.data.staffId }).then(response => {
            wx.hideLoading();
            console.log(response)
            that.setData({
                'staffData': response.data,
            })
            this.getCategoryList1()
        }).catch(response => {
            wx.hideLoading()
        })

    },
    //删除项目
    deletaGoodsItem(e) {
        let dataset = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除当前项目吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '操作中...',
                    })
                    deletaGoodsItem({ goods_id: dataset.id }).then(response => {
                        console.log('分类数据')
                        wx.hideLoading()
                        wx.showToast({
                            icon: '',
                            title: '删除成功',
                        })
                        let arr = that.data.workList;
                        arr.splice(dataset.index, 1)
                        that.setData({
                            workList: arr
                        })
                    }).catch(response => {
                        wx.hideLoading()
                        console.log('数据获取失败')
                        console.log(response)
                    })

                } else if (res.cancel) {

                }

            }
        })
        
    },
    recommendItem(e){
        let dataset = e.currentTarget.dataset;
        this.recommendItemCb(dataset.index)
        
    },
    recommendItemCb(index) {
        let that = this;
        console.log('加载分类')
        let item = this.data.workList[index];
        recommendItem({ goods_id: item.goods_id}).then(response => {
            wx.hideLoading()
            let key = 'workList[' + index + '].is_recommend'
            this.setData({
                [key]: response.data.is_recommend
            })
            let msg='';
            if (response.data.is_recommend==1){
                msg="设置封面成功"
            }
            else{
                msg = "取消封面成功"
            }
            wx.showToast({
                icon:'none',
                title: msg,
            })
        }).catch(response => {
            wx.hideLoading()
            console.log('数据获取失败')
            console.log(response)
        })
    },
    //获取分类数据
    getCategoryList1() {
        let that = this;
        console.log('加载分类')
        getCategoryList({}).then(response => {
            console.log('分类数据')
            wx.hideLoading()
            that.setData({
                'category': response.data.lists,
                'curCatgoryId': response.data.lists[0].id
            })
            this.getStaffWorkList()
        }).catch(response => {
            wx.hideLoading()
            console.log('数据获取失败')
            console.log(response)
        })
    },
    changeCatgory(e){
        let dataset = e.currentTarget.dataset;
        this.setData({
            curCatgoryId: dataset.id,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore':false,
            workList: null
        })
        this.getStaffWorkList()
    },
    changeStatus(e) {
        let dataset = e.currentTarget.dataset;
        console.log(dataset.type)
        this.setData({
            curStatus:dataset.type,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            workList: null
        })
        this.getStaffWorkList()
    },
    lower: function (e) {
        this.getStaffWorkList()
    },
    getStaffWorkList() {
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let that = this;
        let params={
            status: this.data.curStatus,
            category_id: this.data.curCatgoryId,
            store_user_id: this.data.staffData.store_user_id,
            store_id:0,
            page:this.data.pages.curPage,
            sort_field: 'is_recommend'
        }
        getStaffWorkList(params).then(response => {
            console.log(response.data)
            let data = response.data;
            if (that.data.pages.curPage == 1) {
                that.setData({
                    workList: data.lists
                })
            } else {
                that.setData({
                    workList: that.data.workList.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.pages.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            that.setData({
                'pageLoad': false
            })
        })
        
    },
    
})